
const app = getApp()
Page({
  data: {
    typePageNum:1,
    mallPageNum:1,
    showCategoryList:false,
    awardId:0,
    mallsId: '',
    result:[],
    type_list:[],
    goods_list:[],
    totalPage: 0,
    currentType:{}
  },
  onLoad: function (options) {
    this.setData({
      awardId:options.id
    })
    this.getMallType().then(res =>{
      this.setData({
        mallsId: res
      })
      this.getMallList(res)
    })
  },
  onReachBottom() {
    if(this.data.mallPageNum<this.data.totalPage){
      this.setData({
        mallPageNum: this.data.mallPageNum + 1
      })
      wx.showLoading({
        title: '加载更多...',
        icon: 'loading'
      })
      this.getMallList(this.data.mallsId)
    }
  },
  getMallType(){
    return new Promise(resolve =>{
      let _this = this
      app.util.request({
        url: "api/type/findLimit",
        method: "post",
        data: {
          page: _this.data.typePageNum,
          pageSize:10,
      },
        success(response) {
            if (response.data.code == 0) {
                _this.setData({
                   type_list: [..._this.data.type_list,...response.data.data.list],
                   currentType: _this.data.type_list[0]?_this.data.type_list[0]:response.data.data.list[0]
                })
                if(_this.data.typePageNum<response.data.data.pages){
                  _this.data.typePageNum++
                  _this.getMallType()
                }else
                resolve(response.data.data.list[0].id)
            }
        }
    })
    })
  },
  getMallList(id){
    wx.showLoading({
      title: '加载中',
    })
      let _this = this
      app.util.request({
        url: "api/mall/findByTypeId",
        method: "post",
        data: {
          typeId:id,
          page: _this.data.mallPageNum,
          pageSize:10,
      },
        success(response) {
            if (response.data.code == 0) {
                _this.setData({
                  totalPage: response.data.data.pages,
                  goods_list: [..._this.data.goods_list,...response.data.data.list],
                })
            }
        }
    })
  },
  onChange(event) {
    this.setData({
      result: event.detail,
      number:event.detail.length,
    });
  },
  toggle(event) {
    let _this = this;
    let id = event.currentTarget.dataset.item.id;
    let arr = _this.data.result;
    if (arr.indexOf(String(id)) == -1) {
      // 不存在这个id值
      arr.push(String(id))
    } else {
      let index = arr.indexOf(String(id));
      arr.splice(index, 1)
    }
    _this.setData({
      result: arr
    })
    return
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  showCategory(){
    this.setData({
      showCategoryList:true
    })
  },
  chooseCategory(e){
    this.setData({
      mallPageNum: 1,
      goods_list:[],
      currentType : this.data.type_list.find(x => x.id == e.currentTarget.dataset.id),
      showCategoryList:false,
      mallsId: e.currentTarget.dataset.id
    })
    this.getMallList(e.currentTarget.dataset.id)
  },
  toChange(){
    let _this = this
    let mallId = _this.data.result.toString()
    app.util.request({
      url: "api/deal/add",
      method: "post",
      data: {
        awardId:_this.data.awardId,
        mallId:mallId,
        state: 0
      },
      success(response) {
          if (response.data.code == 0) {
            wx.navigateTo({
              url: '/pages/goods/my-goods-index',
            })            
          }
      }
    })
  },
})